Steven A. Assigment 7 - R-Spatial

R Spatial Lab Assignment # 1

Prerequisite: Task 1 Set up a R project for the R-Spatial section.

Task 2: Read the NYC postal areas in Shapefiles into sf objects.

nyc_zip_sf <- st_read("C:/Users/steve/Downloads/G385_R_Data_Viz/R_Spatial/R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `C:\Users\steve\Downloads\G385_R_Data_Viz\R_Spatial\R-Spatial_I_Lab\ZIP_CODE_040114\ZIP_CODE_040114.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 263 features and 12 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
# View the projection (CRS)
st_crs(nyc_zip_sf)
## Coordinate Reference System:
##   User input: NAD83 / New York Long Island (ftUS) 
##   wkt:
## PROJCRS["NAD83 / New York Long Island (ftUS)",
##     BASEGEOGCRS["NAD83",
##         DATUM["North American Datum 1983",
##             ELLIPSOID["GRS 1980",6378137,298.257222101,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4269]],
##     CONVERSION["SPCS83 New York Long Island zone (US survey foot)",
##         METHOD["Lambert Conic Conformal (2SP)",
##             ID["EPSG",9802]],
##         PARAMETER["Latitude of false origin",40.1666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8821]],
##         PARAMETER["Longitude of false origin",-74,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8822]],
##         PARAMETER["Latitude of 1st standard parallel",41.0333333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8823]],
##         PARAMETER["Latitude of 2nd standard parallel",40.6666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8824]],
##         PARAMETER["Easting at false origin",984250,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8826]],
##         PARAMETER["Northing at false origin",0,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8827]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##     USAGE[
##         SCOPE["Engineering survey, topographic mapping."],
##         AREA["United States (USA) - New York - counties of Bronx; Kings; Nassau; New York; Queens; Richmond; Suffolk."],
##         BBOX[40.47,-74.26,41.3,-71.8]],
##     ID["EPSG",2263]]
nyc_zip_sf %>% mapview::mapview()

Task 3: Read and process the NYS health facilities spreadsheet data.

  • Create sf objects from geographic coordinates.
NYS_Health_Facility <- read_csv("C:/Users/steve/Downloads/G385_R_Data_Viz/R_Spatial/R-Spatial_I_Lab/NYS_Health_Facility.csv", 
            show_col_types = FALSE, 
            lazy = FALSE) 


NYS_Health_Facility_points <- NYS_Health_Facility %>%
  rename(
    Facility_Longitude = `Facility Longitude`,
    Facility_Latitude = `Facility Latitude`,
    Facility_Location = `Facility Location`
  ) %>% drop_na(Facility_Longitude) %>% drop_na(Facility_Latitude) %>%
  filter(Facility_Longitude != 0 & Facility_Longitude != 0) %>%
  mutate(
    Facility_Latitude = if_else(Facility_Latitude < 0, 43.21162, Facility_Latitude),
    Facility_Longitude = if_else(Facility_Longitude > 0, -75.45935, Facility_Longitude)
  )

NYS_Health_sf <- st_as_sf(NYS_Health_Facility_points, coords=c('Facility_Longitude','Facility_Latitude'))

st_crs(NYS_Health_sf) <- 4326

NYS_Health_sf %>% mapview::mapview()

Task 4: Read and process the NYS retail food stores data.

  • Create sf objects from geographic coordinates for NYC.
Retail_Food_xy <- read_csv("C:/Users/steve/Downloads/G385_R_Data_Viz/R_Spatial/R-Spatial_I_Lab/nys_retail_food_store_xy.csv", 
                                show_col_types = FALSE, 
                                lazy = FALSE) 

# For all NYS
# Drop NA coords
Retail_Food_xy_points <- Retail_Food_xy %>% drop_na('Y') %>% 
  drop_na('X') 

Retail_food_sf <- st_as_sf(Retail_Food_xy_points, coords=c('X','Y'))
st_crs(Retail_food_sf) <- 4326

# NYC Food/Retail
city_list = c('BROOKLYN', 'BRONX', 'QUEENS', 'MANHATTAN','STATEN ISLAND',
              'NEW YORK','ASTORIA', 'ELMHURST', 'FLUSHING', 'LONG ISLAND CITY', 
              'JACKSON HEIGHTS', 'SUNNYSIDE', 'WOODSIDE', 'FOREST HILLS', 'REGO PARK', 
              'JAMAICA', 'RIDGEWOOD', 'FRESH MEADOWS', 'BAYSIDE', 'COLLEGE POINT', 
              'WHITESTONE', 'OZONE PARK', 'RICHMOND HILL', 'KEW GARDENS', 'KEW GARDENS HILLS', 
              'HOLLIS', 'ST. ALBANS', 'SPRINGFIELD GARDENS', 'CAMBRIA HEIGHTS', 
              'LAURELTON', 'ROSEDALE', 'BELLEROSE', 'DOUGLASTON', 'LITTLE NECK', 
              'GLEN OAKS', 'MIDDLE VILLAGE', 'MASPETH', 'CORONA', 'QUEENS VILLAGE', 
              'ROCKAWAY BEACH', 'ROCKAWAY POINT', 'ROCKAWAY PARK','FAR ROCKAWAY', 'BREEZY POINT', 
              'ARVERNE', 'HOLLISWOOD', 'SOUTH OZONE PARK', 'HOWARD BEACH'
              )
Retail_Food_nyc_points <- Retail_Food_xy %>% 
  filter(City %in% city_list) %>% 
  drop_na('Y') %>% 
  drop_na('X') 

Retail_food_nyc_sf <- st_as_sf(Retail_Food_nyc_points, coords=c('X','Y'))
st_crs(Retail_food_nyc_sf) <- 4326

# Reproject to 2263, nyc projection
Retail_food_nyc_2263_sfc <- st_transform(Retail_food_nyc_sf, 2263)

# check rerojection
st_crs(Retail_food_nyc_2263_sfc)
## Coordinate Reference System:
##   User input: EPSG:2263 
##   wkt:
## PROJCRS["NAD83 / New York Long Island (ftUS)",
##     BASEGEOGCRS["NAD83",
##         DATUM["North American Datum 1983",
##             ELLIPSOID["GRS 1980",6378137,298.257222101,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4269]],
##     CONVERSION["SPCS83 New York Long Island zone (US survey foot)",
##         METHOD["Lambert Conic Conformal (2SP)",
##             ID["EPSG",9802]],
##         PARAMETER["Latitude of false origin",40.1666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8821]],
##         PARAMETER["Longitude of false origin",-74,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8822]],
##         PARAMETER["Latitude of 1st standard parallel",41.0333333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8823]],
##         PARAMETER["Latitude of 2nd standard parallel",40.6666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8824]],
##         PARAMETER["Easting at false origin",984250,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8826]],
##         PARAMETER["Northing at false origin",0,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8827]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##     USAGE[
##         SCOPE["Engineering survey, topographic mapping."],
##         AREA["United States (USA) - New York - counties of Bronx; Kings; Nassau; New York; Queens; Richmond; Suffolk."],
##         BBOX[40.47,-74.26,41.3,-71.8]],
##     ID["EPSG",2263]]
# Retail_food_nyc_2263_sfc %>% mapview::mapview()

Task 5: Use simple mapping method such as mapview with a basemap to verify the above datasets in terms of their geometry locations.

urLz = "file:///C:/Users/steve/Downloads/G385_R_Data_Viz/R_Spatial/nys_retailfood.html" 

knitr::include_url(urLz, height = "500px")

Task 6: Save the three sf objects in a RData file or in a single GeoPackage file/database.

  • encoding issue, does not allow for the lines below to be added as cell
st_write(nyc_zip_sf, 
         dsn = "./data/nys/nys.gpkg", 
         layer='nyc_zipcode',
         delete_layer = T)
## Deleting layer `nyc_zipcode' using driver `GPKG'
## Writing layer `nyc_zipcode' to data source `./data/nys/nys.gpkg' using driver `GPKG'
## Writing 263 features with 12 fields and geometry type Polygon.
st_write(NYS_Health_sf, 
         dsn = "./data/nys/nys.gpkg", 
         layer='NYS_Health_Facilities',
         delete_layer = T)
## Deleting layer `NYS_Health_Facilities' using driver `GPKG'
## Writing layer `NYS_Health_Facilities' to data source 
##   `./data/nys/nys.gpkg' using driver `GPKG'
## Writing 3844 features with 34 fields and geometry type Point.
st_write(Retail_food_sf, 
         dsn = "./data/nys/nys.gpkg", 
         layer='NYS_Retail_Food',
         delete_layer = T)
## Deleting layer `NYS_Retail_Food' using driver `GPKG'
## Writing layer `NYS_Retail_Food' to data source 
##   `./data/nys/nys.gpkg' using driver `GPKG'
## Writing 23972 features with 16 fields and geometry type Point.
st_write(Retail_food_nyc_2263_sfc, 
         dsn = "./data/nys/nys.gpkg", 
         layer='NYC_Retail_Food',
         delete_layer = T)
## Deleting layer `NYC_Retail_Food' using driver `GPKG'
## Writing layer `NYC_Retail_Food' to data source 
##   `./data/nys/nys.gpkg' using driver `GPKG'
## Writing 11258 features with 16 fields and geometry type Point.